package com.bdqn.controller;

import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONObject;
import com.bdqn.pojo.SysRole;
import com.bdqn.pojo.SysUser;
import com.bdqn.service.SysRoleService;
import com.bdqn.service.SysUserService;
import com.bdqn.util.Constants;
import com.bdqn.util.Page;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * 用户控制类
 *
 * @aurhor Administrator  whs
 * @since 2024/7/30
 */
@Controller
@RequestMapping(value = "/role")
public class SysRoleController {

    @Resource
    private SysRoleService sysRoleService;

    private Logger logger = Logger.getLogger(SysRoleController.class);


    /**
     * 查询角色信息
     */
    @GetMapping("/list")
    public String list(Model model, @RequestParam(value = "roleName",required = false) String roleName , @RequestParam(value = "pageIndex",required = false,defaultValue = "1") Integer pageIndex,@RequestParam(value = "pageSize",required = false) Integer pageSize) {
        try {
            List<SysRole> roleList = sysRoleService.list();

            model.addAttribute("roleList", roleList);   // 填充角色下拉列表

        }catch (Exception e) {
            e.printStackTrace();
            return "redirect:/frame";
        }
        return "sysRole/list";
    }


    /**
     * 去到添加角色页面
     */
    @GetMapping("/add")
    public String toAdd(Model model) {
//        // 获取所有角色信息填充下拉列表
//        List<SysRole> roleList = sysRoleService.list();
//        model.addAttribute("roleList", roleList);
        return "sysRole/add";
    }

    /**
     * 添加角色信息
     * @param model
     * @return
     */
    @PostMapping("/add")
    public String add(Model model,HttpSession session, SysRole sysRole , SysUser sysUser){
        SysUser loginSysUser = (SysUser) session.getAttribute(Constants.USER_SESSION);
        sysRole.setCreatedUserId((long) loginSysUser.getId());
        sysRole.setCreatedTime(new Date());
        boolean flag = sysRoleService.addSysRole(sysRole);
        if (flag){ // 成功重定向到列表页
            return "redirect:/role/list";
        }

        model.addAttribute("error","添加失败！");
        return "sysRole/add";
    }


    /**
     * 跳转到修改角色信息页面
     */
    @GetMapping("/toUpdate")
    public String toUpdate(@RequestParam Integer roleId, Model model) {
        logger.debug("跳转到修改角色信息页面");
        SysRole sysRole = sysRoleService.getUserById(roleId);
        model.addAttribute(sysRole);
        return "sysRole/update";
    }

    /**
     *修改角色信息
     */
    @PostMapping("/update")
    public String modifySave(HttpSession session,SysUser sysUser,SysRole sysRole) {
        sysRole.setUpdatedUserId((long) ((SysUser)session.getAttribute(Constants.USER_SESSION)).getId());
        sysRole.setUpdatedTime(new SimpleDateFormat("yyyy-MM-dd").format(new Date()));
        if (sysRoleService.updateSysRoleId(sysRole)){
            return "redirect:/role/list";
        }
        return "sysRole/update";
    }

    /**
     * 使用 Ajax 异步加载 角色信息
     */
    @ResponseBody
    @GetMapping("/ajaxList")
    public String ajaxList() {
        List<SysRole> list = sysRoleService.list();
        return JSONObject.toJSONString(list);
    }





}
